//
//  HelloWorldLayer.m
//  zhuan
//
//  Created by zhao ys on 5/27/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"
#import "playLayer.h"
#import "settingLayer.h"
#import "SoundManager.h"
#import "GameCenter.h"
#import "modeSelectLayer.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

-(CCMenuItem *) menuWithLabel:(NSString*)label target:(id)target selector:(SEL)selector {
	CCSprite * button_normal = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_unprass.png"]];
	CCSprite * button_select = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_onprass.png"]];
	CCMenuItem * button = [CCMenuItemSprite itemFromNormalSprite:button_normal selectedSprite:button_select target:target selector:selector];
	CCLabelTTF * l = [CCLabelTTF labelWithString:label fontName:@"Marker Felt" fontSize:32]; 
	l.color = ccc3(243, 189, 69);
	CGSize bSize = button.contentSize;
	l.position = ccp(bSize.width / 2, bSize.height / 2 + 5);
	[button addChild:l];
	return button;
}

-(CCMenuItem *) menuWithPng:(NSString*)png target:(id)target selector:(SEL)selector {
	CCSprite * button_normal = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:png]];
	CCSprite * button_select = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:png]];
	CCMenuItem * button = [CCMenuItemSprite itemFromNormalSprite:button_normal selectedSprite:button_select target:target selector:selector];
	return button;
}


// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
		CGSize size = [[CCDirector sharedDirector] winSize];
		
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"dkz.plist"];
		
		//load back ground
		CCSpriteFrame * bgframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_startmenu_bg.png"];
		CCSprite * bg = [CCSprite spriteWithSpriteFrame:bgframe];
		bg.position = ccp(size.width/2, size.height/2);
		[self addChild:bg z:-1];
	
		//logo
		logo_ = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_title.png"]];
		[self addChild:logo_ z:0 tag:7];				

		
		//game center
		{
			trinkets1_ = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			CGSize tSize = trinkets1_.contentSize;
			[self addChild:trinkets1_ z:0 tag:8];
			
			CCSprite * trinkets1_a = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets1_a.position = ccp(tSize.width/2, tSize.height*3/2);
			[trinkets1_ addChild:trinkets1_a];
			gamecenter_ = [self menuWithPng:@"dkz_leaderboards.png" target:self selector:@selector(OnMenuGameCenter:)];
			gamecenter_.anchorPoint = ccp(0.5, 0.9);
			gamecenter_.position = ccp(tSize.width/2, 400);
			CCMenuItem * menu_g = [CCMenu menuWithItems:gamecenter_,nil];
			menu_g.position = ccp(0,0);
			[trinkets1_ addChild:menu_g];
		}
		
		
		//more game
		{	
			trinkets2_ = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			CGSize tSize = trinkets2_.contentSize;
			[self addChild:trinkets2_ z:0 tag:9];
			CCSprite * trinkets2_a = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets2_a.position = ccp(tSize.width/2, tSize.height*3/2);
			[trinkets2_ addChild:trinkets2_a];
			CCSprite * trinkets2_b = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_001.png"]];
			trinkets2_b.position = ccp(tSize.width/2, -tSize.height/2);
			[trinkets2_ addChild:trinkets2_b];
			
			moregame_ = [self menuWithPng:@"dkz_moregame.png" target:self selector:@selector(OnMenuMoreGame:)];
			moregame_.anchorPoint = ccp(0.5, 0.9);
			moregame_.position = ccp(tSize.width/2, 400);
			CCMenuItem * menu_g = [CCMenu menuWithItems:moregame_,nil];
			menu_g.position = ccp(0,0);
			[trinkets2_ addChild:menu_g];
		}
		
		//menu
        if ([[GameLogic sharedGameLogic] havesave]) {
            CCMenuItem * play = [self menuWithLabel:@"play" target:self selector:@selector(OnMenuPlay:)];
            CCMenuItem * continue_ = [self menuWithLabel:@"continue" target:self selector:@selector(OnMenuContinue:)];
            CCMenuItem * setting = [self menuWithLabel:@"option" target:self selector:@selector(OnMenuSetting:)];
	
		//play.position = ccp(size.width / 2, size.height / 2 - 30);
		//setting.position = ccp(size.width / 2, size.height / 2 - 132);

            CCMenu * menu = [CCMenu menuWithItems:play, continue_, setting, nil];
            [menu  alignItemsVerticallyWithPadding:-30];
            menu.position = ccp(size.width/2, size.height / 2 - 150);
            [self addChild: menu];
        } else {
            CCMenuItem * play = [self menuWithLabel:@"play" target:self selector:@selector(OnMenuPlay:)];
            CCMenuItem * setting = [self menuWithLabel:@"option" target:self selector:@selector(OnMenuSetting:)];
            
            //play.position = ccp(size.width / 2, size.height / 2 - 30);
            //setting.position = ccp(size.width / 2, size.height / 2 - 132);
            
            CCMenu * menu = [CCMenu menuWithItems:play, setting, nil];
            [menu  alignItemsVerticallyWithPadding:-30];
            menu.position = ccp(size.width/2, size.height / 2 - 100);
            [self addChild: menu];
        }
	}
	return self;
}

-(void) onEnter {
	NSLog(@"HelloWorldLayer OnEnter");
	[super onEnter];
	
	CGSize size = [[CCDirector sharedDirector] winSize];
	
	CGSize lSize = logo_.contentSize;
	logo_.position = ccp(size.width/2, size.height + lSize.height / 2);
	[logo_ runAction: [CCEaseElasticOut actionWithAction:[CCMoveTo actionWithDuration:2 position:ccp(size.width/2,580)] period:0.8]];
	
	int trinket_x = 140;
	trinkets1_.position = ccp(trinket_x, 100); //size.height / 2);
	[trinkets1_ runAction:[CCEaseElasticOut actionWithAction:
						   [CCMoveTo actionWithDuration:5 position:ccp(trinkets1_.position.x, size.height/2 - 100)] period:0.3f]];
	
	gamecenter_.rotation = 5;
	[gamecenter_ runAction:[CCEaseElasticOut actionWithAction:[CCRotateTo actionWithDuration:100 angle:0] period:0.04f]];
	
	
	trinkets2_.position = ccp(size.width - trinket_x, 100); //size.height / 2);
	[trinkets2_ runAction:  [CCEaseElasticOut actionWithAction:
							[CCMoveTo actionWithDuration:8 position:ccp(trinkets2_.position.x, size.height/2 - 100)] period:0.3f]];
	
	moregame_.rotation = -5;
	[moregame_ runAction: [CCEaseElasticOut actionWithAction:[CCRotateTo actionWithDuration:100 angle:0] period:0.035f]];
	
	[[SoundManager sharedManager] stop:@"background"];
	if (music_on) {
		[[SoundManager sharedManager] play:@"background_menu" loops:-1];
	}
}

-(void) OnMenuPlay:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"click" loops:1];

	/*
	[GameLogic ClassicMode];
	[GameLogic ResetGameConfig];
	
	[[GameLogic sharedGameLogic] gen];
	
	[[CCDirector sharedDirector] replaceScene:
		[CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
	 */
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[modeSelectLayer scene]]];
}

/*
-(void) OnMenuEndlessMode:(id)sender {	
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];
	
	[[GameLogic sharedGameLogic] gen];
	[GameLogic EndlessMode];
	[GameLogic ResetGameConfig];
	
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
}

-(void) OnMenuScoreMode:(id)sender {	
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];

	[[GameLogic sharedGameLogic] gen];
	[GameLogic ScoreMode];
	[GameLogic ResetGameConfig];
	
	[[CCDirector sharedDirector] replaceScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
}
*/

-(void) OnMenuContinue:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"click" loops:1];
    
    if ([[GameLogic sharedGameLogic] load] != -1) {
        [[CCDirector sharedDirector] replaceScene:
         [CCTransitionFade transitionWithDuration:0.5f scene:[playLayer scene]]];
    } else {
    	if (sound_on) [[SoundManager sharedManager] play:@"failed" loops:1];
    }
}

-(void) OnMenuSetting:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];
	
	[[CCDirector sharedDirector] pushScene:
	 [CCTransitionFade transitionWithDuration:0.5f scene:[settingLayer scene]]];	
}

-(void) OnMenuGameCenter:(id)sender {
	if (game_center_on && [[GameCenter sharedGameCenter] isAuthenticated]) {
		[(CCNode*)sender runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.3 scale:0.9], [CCScaleTo actionWithDuration:0.1 scale:1], nil]];
		if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];
		[[GameCenter sharedGameCenter] enterLeaderboard:@"iButtons_1" withView:[[CCDirector sharedDirector] openGLView]];
	}
}

-(void) OnMenuMoreGame:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];
		
	[(CCNode*)sender runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.3 scale:0.9], [CCScaleTo actionWithDuration:0.1 scale:1], nil]];

	//NSURL * url = [NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=443651716&mt=8"];
	
	
	NSURL * url = [NSURL URLWithString:@"http://itunes.apple.com/artist/legend-studio/id419797040"];
	[[UIApplication sharedApplication] openURL:url];
}


// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
